<?php
/**************************************************************************
 * Naanal PHP Framework, Simple, Efficient and Developer Friendly
 * Ver 3.0, Copyright (C) <2010>  <Tamil Amuthan. R>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ************************************************************************/

class ClsNaanalAuthentication
{
    public $controlPanel=null;
    public $fldUserID="userid";
    public $fldUser="name";
    public $fldSessionID="sessionid";
    public $fldPassword="password";
    public $locked=false;

    private $pdo=null;
    private $objTable=null;
    private $tableName=null;
    private $objSession=null;
    private $errUnexpected="unexpected error. please contact your software vendor";

    function __construct(&$pdo,$tableName)
    {
        $this->pdo=$pdo;
        $this->tableName=$tableName;
    }

    function setControlPanel($controlPanel)
    {
        $this->controlPanel=$controlPanel;		
    }

    function logout()
    {
        $objNaanalSession=  ClsNaanalSession::getInstance();
        $loginID=$objNaanalSession->getLoginID();
        $stat=$this->pdo->setQuery("update $this->tableName set `$this->fldSessionID`='' where id={$loginID}");
        $objNaanalSession->logout();
        if(!ClsConfig::$DISABLE_TRACKING)
        {
            $obj=ClsWrapper::getInstance("tracking");
            $obj->set("user_id",$loginID);
            $obj->set("sessionid",session_id());
            $obj->set("url",getCurrentURL());
            $obj->set("page","login");
            $obj->set("loginip",getUserIP());
            $obj->set("actiontime",time());
            $obj->update();
        }
    }

    function retrievePassword()
    {

    }

    function changePassword()
    {
        
    }

    function authenticate($user,$password)
    {
        $objNaanalSession=ClsNaanalSession::getInstance();
        if($objNaanalSession->getWebPath() && $objNaanalSession->getWebPath()!=WEB_PATH)
        {
            traceDirect($objNaanalSession->getWebPath()." ".WEB_PATH);
        }
        $objNaanalRequest=ClsNaanalRequest::getInstance();
        $stat=$this->pdo->setQuery("select * from {$this->tableName} where `{$this->fldUserID}`='{$user}' and `{$this->fldPassword}`='{$password}'");
        $arrRow=$this->pdo->getAllAssoc();            
        if(count($arrRow)<=0)
        {
            errMsg("LOGIN_ERROR");
            return false;
        }
        else
        {
            if($arrRow[0]["active"]>0)
            {
                $objNaanalSession->setData("loginpanel",$this->controlPanel);
                $objNaanalSession->setPanelData("apppath",APP_PATH);
                $objNaanalSession->setPanelData("webpath",WEB_PATH);
                $objNaanalSession->setPanelData("fldUserID",$this->fldUserID);
                $objNaanalSession->setPanelData("loginname",$user);
                $objNaanalSession->setPanelData("user",$arrRow[0][$this->fldUser]);
                $objNaanalSession->setPanelData("loginid",$arrRow[0]["id"]);
                $objNaanalSession->setPanelData("isadmin",$arrRow[0]["isadmin"]);
                $objNaanalSession->setPanelData("NAANAL_LOGIN_TIME",time());
                if(isset($arrRow[0]["client_id"]))
                {
                    $objNaanalSession->setPanelData("client_id",$arrRow[0]["client_id"]);
                    $this->pdo->setQuery("select * from client where id={$arrRow[0]["client_id"]}");
                    $arrRowClient=$this->pdo->getAllAssoc();
                    $objNaanalSession->setPanelData("client_company",$arrRowClient[0]["company"]);
                }
                if(isset($arrRow[0]["ismanage"])) 
                    $objNaanalSession->setPanelData("ismanage",$arrRow[0]["ismanage"]);
                /**
                 * adding the custom data to the session from ClsConfig.php 
                 */
                if(!empty(ClsConfig::$arrLoginInfo))
                {
                    foreach(ClsConfig::$arrLoginInfo as $k=>$v)
                    {
                        if($k=="request")
                        {
                            $objNaanalSession->setPanelData($v,$objNaanalRequest->getData($v));
                        }
                        else if($k=="logintable")
                        {
                            $objNaanalSession->setPanelData($v,$arrRow[0][$v]);
                        }
                    }
                }
                if(function_exists("onLogin"))
                {
                    onLogin();
                }
                if(!ClsConfig::$DISABLE_TRACKING)
                {
                    $obj=ClsWrapper::getInstance("tracking");
                    $obj->set("user_id",$arrRow[0]["id"]);
                    $obj->set("sessionid",session_id());
                    $obj->set("url",getCurrentURL());
                    $obj->set("page","login");
                    $obj->set("loginip",getUserIP());
                    $obj->set("actiontime",time());
                    $obj->update();
                }
                errMsg("");
                return true;
            }
            else
            {
                $this->pdo->statement=null;
                errMsg("ACCOUNT_INACTIVE_USER");
                return false;
            }
        }	    
    }
}
?>